package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.SwitchButton;

/**
 * 个人中心->我的设置
 * 
 * @author G
 * 
 */
public class CenterSettingsActivity extends BaseActivity {
	private TextView tv_title_title, tv_title_back;
	private SwitchButton sb_drug;
	private SwitchButton sb_drug_manager;
	private Button bt_quanxian;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_center_settings);
		initView();

	}

	private void initView() {
		initTitleBar();
		getDate();
		sb_drug = (SwitchButton) findViewById(R.id.sb_drug);
		sb_drug.setOnClickListener(this);
		sb_drug_manager = (SwitchButton) findViewById(R.id.sb_drug_manager);
		sb_drug_manager.setOnClickListener(this);
		bt_quanxian = (Button) findViewById(R.id.bt_quanxian);
		bt_quanxian.setOnClickListener(this);

		SharedPreferences sp = getSharedPreferences(MyConst.getACCOUNT(),
				MODE_PRIVATE);
		String myMin = sp.getInt("myMind", 0) + "";
		String mym = sp.getInt("autoAuth", 0) + "";
		if (myMin.equals("")) {
			if (myMind.equals("1")) {
				sb_drug.changeOpen();
			}
			if (autoAuth.equals("1")) {
				sb_drug_manager.changeOpen();
			}

		} else {

			if (myMin.equals("1")) {
				sb_drug.changeOpen();
			}
			if (mym.equals("1")) {
				sb_drug_manager.changeOpen();
			}
		}
	}

	private void getDate() {
		myMind = getIntent().getStringExtra("myMind");
		autoAuth = getIntent().getStringExtra("autoAuth");

	}

	public void initTitleBar() {
		tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.myset));
		tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
	}

	private Handler Handler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {

			if (state.equals("001")) {

				finish();
				Toast.makeText(CenterSettingsActivity.this, "操作成功", 0).show();
			} else {
				Toast.makeText(CenterSettingsActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CenterSettingsActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private String myMind;
	private String autoAuth;

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();

			break;
		case R.id.sb_drug:
			sb_drug.changeOpen();
			break;
		case R.id.sb_drug_manager:
			sb_drug_manager.changeOpen();
			break;
		case R.id.bt_quanxian:
			HashMap<String, Object> map = new HashMap<String, Object>();
			Context ctx = CenterSettingsActivity.this;
			String account = MyConst.getACCOUNT();
			SharedPreferences sp = ctx.getSharedPreferences(account,
					MODE_PRIVATE);
			Editor edit = sp.edit();
			edit.putString("aliasAccount", account);
			map.put("aliasAccount", account);
			if (sb_drug.open != true) {
				edit.putInt("myMind", 1);
				map.put("myMind", 1);
			} else {
				edit.putInt("myMind", 0);
				map.put("myMind", 0);
			}
			if (sb_drug_manager.open != true) {
				edit.putInt("autoAuth", 1);
				map.put("autoAuth", 1);
			} else {
				edit.putInt("autoAuth", 0);
				map.put("autoAuth", 0);
			}

			edit.commit();
			TransferParameter parameter = new TransferParameter();
			parameter.setUsername("");
			parameter.setPassword(" ");
			parameter.setCurrentPage("");
			parameter.setPagesize("");
			parameter.setData(map);
			new HttpServer()
					.post(HttpCons.changeMySettings, parameter, Handler);
			break;
		}
	}

}
